package com.terabits.smartriver.controller;

import com.terabits.smartriver.constants.AuthorityEnum;
import com.terabits.smartriver.constants.result.BaseResp;
import com.terabits.smartriver.constants.result.ResultStatus;
import com.terabits.smartriver.meta.po.Role;
import com.terabits.smartriver.meta.po.User;
import com.terabits.smartriver.service.AuthorityService;
import com.terabits.smartriver.service.UserService;
import com.terabits.smartriver.util.PageData;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 账户管理接口
 */
@CrossOrigin
@RestController
public class AccountController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private AuthorityService authorityService;

    @ApiOperation(value = "账户 查询", tags = {"账户管理"})

    @RequestMapping(value = "/account-mgt/account/get", method = RequestMethod.GET)
    public BaseResp getUser() {
        return new BaseResp(ResultStatus.SUCCESS, userService.getUser(this.getPageData()));
    }

    @ApiOperation(value = "账户 添加", tags = {"账户管理"})
    @RequestMapping(value = "/account-mgt/account/add", method = RequestMethod.POST)
    public BaseResp addUser(User user, @RequestParam("roleIds") List<Integer> roleIds) {
        int addRes = userService.addUser(user);
        if (addRes == -1) {
            return new BaseResp(ResultStatus.error_duplicated_data);
        }
        if (user.getId() != null) {
            authorityService.updateUserRole(user.getId(), roleIds);
            return new BaseResp(ResultStatus.SUCCESS);
        }

        return new BaseResp(ResultStatus.FAIL);
    }

    @ApiOperation(value = "账户 修改", tags = {"账户管理"}, notes = "账户 修改")
    @RequestMapping(value = "/account-mgt/account/update", method = RequestMethod.POST)
    public BaseResp updateUser(User user) {
        return new BaseResp(ResultStatus.SUCCESS, userService.updateUser(user));
    }

    @ApiOperation(value = "账户 删除", tags = {"账户管理"}, notes = "账户 删除")
    @RequestMapping(value = "/account-mgt/account/delete", method = RequestMethod.POST)
    public BaseResp deleteUser(Integer userId) {
        return new BaseResp(ResultStatus.SUCCESS, userService.deleteUser(userId));
    }

    @ApiOperation(value = "用户-角色分配关系 查询", tags = {"账户管理-权限管理"})
    @RequestMapping(value = "/account-mgt/authority/user-role/get", method = RequestMethod.GET)
    public BaseResp getUserRole() {
        return new BaseResp(ResultStatus.SUCCESS, authorityService.getUserRole());
    }

    @ApiOperation(value = "用户-角色分配关系 修改", tags = {"账户管理-权限管理"})
    @RequestMapping(value = "/account-mgt/authority/user-role/update", method = RequestMethod.POST)
    public BaseResp updateUserRole(Integer userId, @RequestParam List<Integer> roleIds) {
        return new BaseResp(ResultStatus.SUCCESS, authorityService.updateUserRole(userId, roleIds));
    }

    @ApiOperation(value = "角色 查询", tags = {"账户管理-权限管理"})
    @RequestMapping(value = "/account-mgt/authority/role/get", method = RequestMethod.GET)
    public BaseResp getRole() {
        return new BaseResp(ResultStatus.SUCCESS, authorityService.getRole());
    }

    @ApiOperation(value = "角色 添加", tags = {"账户管理-权限管理"})
    @RequestMapping(value = "/account-mgt/authority/role/add", method = RequestMethod.POST)
    public BaseResp addRole(Role role) {
        int addRes = authorityService.addRole(role);
        if (addRes == -1) {
            return new BaseResp(ResultStatus.error_duplicated_data);
        }
        return new BaseResp(ResultStatus.SUCCESS);
    }

    @ApiOperation(value = "角色 删除", tags = {"账户管理-权限管理"})
    @RequestMapping(value = "/account-mgt/authority/role/delete", method = RequestMethod.POST)
    public BaseResp deleteRole(Integer roleId) {
        return new BaseResp(ResultStatus.SUCCESS, authorityService.deleteRole(roleId));
    }

    @ApiOperation(value = "权限 查询", tags = {"账户管理-权限管理"})
    @RequestMapping(value = "/account-mgt/authority/menu/get", method = RequestMethod.GET)
    public BaseResp getMenu() {
        return new BaseResp(ResultStatus.SUCCESS, authorityService.getMenu(AuthorityEnum.MENU_TYPE_PC.getType(), null));
    }

    @ApiOperation(value = "角色-权限分配关系 添加", tags = {"账户管理-权限管理"})
    @RequestMapping(value = "/account-mgt/authority/role-menu/update", method = RequestMethod.POST)
    public BaseResp updateRoleMenu(Integer roleId, @RequestParam List<Integer> menuIds) {
        authorityService.updateRoleMenu(AuthorityEnum.MENU_TYPE_PC.getType(), roleId, menuIds);
        return new BaseResp(ResultStatus.SUCCESS);
    }

    @ApiOperation(value = "角色-权限分配关系 查询", tags = {"账户管理-权限管理"})
    @RequestMapping(value = "/account-mgt/authority/role-menu/get", method = RequestMethod.GET)
    public BaseResp getRoleMenu() {
        return new BaseResp(ResultStatus.SUCCESS, authorityService.getRoleMenu(AuthorityEnum.MENU_TYPE_PC.getType()));
    }
}
